package com.dt76.smallloanbackenduser.controller.etao;


import com.dt76.smallloanbackenduser.pojo.etao.Department;
import com.dt76.smallloanbackenduser.pojo.etao.Role;
import com.dt76.smallloanbackenduser.remote.etao.PrivilegeManagement;
import com.dt76.smallloanbackenduser.remote.etao.RoleManageRemote;
import com.dt76.smallloanbackenduser.utils.Page;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleManageController {

    @Autowired
    RoleManageRemote roleManageRemote;
    //注入部门的远程调用
    @Autowired
    PrivilegeManagement privilegeManagement;

    //获取角色列表返回值页面
    @RequestMapping("/getRole")
    public String getRole(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        Page page = roleManageRemote.getAllRole(pageNum);
        List<Department> deptList = privilegeManagement.getDept();
        List<Role> allRole = page.getRows();
        List<Integer> pageArr = new ArrayList<Integer>();
        for (int i = 0; i < page.getTotalPageCount(); i++) {
            pageArr.add(i+1);
        }
        model.addAttribute("allRole", allRole);
        model.addAttribute("pageInfo", page);
        model.addAttribute("pageArr", pageArr);
        model.addAttribute("deptList", deptList);
        return "pages/etao/roleManage";
    }

    //ajax请求的
    @RequestMapping("/getPage")
    @ResponseBody
    public String getPage(Integer pageNum) {
        return "/role/getRole?pageNum="+pageNum;
    }


    //    根据角色id获取角色
    @RequestMapping("/getRoleById")
    @ResponseBody
    public Role getRoleById(String rId) {
        return roleManageRemote.getRoleById(Integer.valueOf(rId));
    }
    //    根据部门id获取角色
    @RequestMapping("/getRoleByDid")
    @ResponseBody
    public List<Role> getRoleByDid(String dId) {
        return roleManageRemote.getRoleByDid(Integer.valueOf(dId));
    }

    //    删除角色
    @RequestMapping("/delRoleById")
    @ResponseBody
    public Result delRoleById(String rId) {
        Result result = roleManageRemote.delRole(Integer.valueOf(rId));
        if (result.isSuccess()) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
        }
        return result;
    }

    //修改&新增角色
    @RequestMapping("/edit")
    @ResponseBody
    public Result editRole(Role role, String roleId) {
        Result result = new Result();
        if ("0".equals(roleId)) {
            //走添加
            result = roleManageRemote.addRole(role);
            if (result.isSuccess()) {
                result.setMsg("添加成功");
            } else {
                result.setMsg("添加失败");
            }
        } else {
            //走修改
            role.setrId(Integer.valueOf(roleId));
            result = roleManageRemote.updateRole(role);
            if (result.isSuccess()) {
                result.setMsg("修改成功");
            } else {
                result.setMsg("修改失败");
            }
        }
        return result;
    }


}
